// pages/home/home.js
var goodsData=require('../../utils/data.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    background:[
      '../../images/ban1.jpg',
      '../../images/ban2.jpg',
      '../../images/ban3.jpg'
    ],
    goodsShowList:[]
  },
// 购物车开始
toCart(e){
  var index = e.currentTarget.dataset.id
  var isCart = this.data.goodsShowList[index].isCart
  if(!isCart){
    var oper ="goodsShowList["+index+"].isCart"
    this.setData({
      [oper]:true
    })
    var cartList = wx.getStorageSync('cartList') || []

    cartList.push({
      index:index,
      count:1
    })

    wx.setStorageSync('cartList', cartList)
  }

},

// 收藏开始
  toLove(e){
    console.log(e);
    var index = e.currentTarget.dataset.id
    var isLove = this.data.goodsShowList[index].isLove
    var oper ="goodsShowList["+index+"].isLove"
    this.setData({
      [oper]:!isLove
    })
    var loveList = wx.getStorageSync("loveList")||[]
    if(!isLove){
      loveList.push(index)
    }else{
      for(let i=0;i<loveList.length;i++){
        if(loveList[i] === index){
          loveList.splice(i,1)
        }
      }
    }
    wx.setStorageSync("loveList", loveList)
  },
// 收藏结束


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var goodsList = goodsData.getGoodsList()
    var goodsShowList = []
    var loveList = wx.getStorageSync("loveList")
    for(let i=0;i<goodsList.length;i++){
      goodsShowList.push(
        {
          detail:goodsList[i],
          isLove:false,
          isCart:false
        }
      )
    }
    for(let i=0;i<loveList.length;i++){
      var index = loveList[i];
      goodsShowList[index].isLove = true;
    }
    this.setData({
      goodsShowList:goodsShowList
    })
  },
})